﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class AdminAddNewCar : Form
    {

        SqlConnection dbConnection2;

        public AdminAddNewCar()
        {
            SqlConnector connection = new SqlConnector();
            dbConnection2 = connection.getInstance();
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void addNewCarButton_Click(object sender, EventArgs e)
        {
            bool fieldsFilledOut = true;

            try
            {
                int test = Convert.ToInt32(engineNumberBox.Text.Trim().ToString());
                int test2 = Convert.ToInt32(capacityBox.Text.Trim().ToString());
                int test3 = Convert.ToInt32(sweptVolumeBox.Text.Trim().ToString());
            }
            catch (FormatException ex)
            {
                fieldsFilledOut = false;
            }


            if (carTypeBox.Text.Trim() == String.Empty)
            {
                carTypeError.Text = "Podaj rodzaj samochodu";
                carTypeError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (capacityBox.Text.Trim() == String.Empty)
            {
                capacityError.Text = "Podaj pojemność samochodu";
                capacityError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (engineNumberBox.Text.Trim() == String.Empty)
            {
                engineNumberError.Text = "Podaj numer silnika";
                engineNumberError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (sweptVolumeBox.Text.Trim() == String.Empty)
            {
                sweptVolumeError.Text = "Podaj pojemność skokową";
                sweptVolumeError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (brandBox.Text.Trim() == String.Empty)
            {
                brandError.Text = "Podaj markę samochodu";
                brandError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (modelBox.Text.Trim() == String.Empty)
            {
                modelError.Text = "Podaj model samochodu";
                modelError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if ( (fieldsFilledOut) &&  (!checkIfRecordExists(engineNumberBox.Text.Trim().ToString(), "Vehicles")  ))
            {
                dbConnection2.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Vehicles (licence_ID, car_type, capacity, engine_number, swept_volume, brand, model) VALUES (@licence_ID, @car_type, @capacity, @engine_number, @swept_volume, @brand, @model)");
                cmd.Connection = dbConnection2;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@car_type", carTypeBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@capacity", capacityBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@engine_number", engineNumberBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@swept_volume", sweptVolumeBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@brand", brandBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@model", modelBox.Text.Trim().ToString());
                string spoko = licenseIDBox.SelectedItem.ToString();
                int licenceID = 1;
                switch (spoko)
                {
                    case "A":
                        licenceID = 1;
                        break;
                    case "B":
                        licenceID = 2;
                        break;
                    case "C":
                        licenceID = 3;
                        break;
                }
                cmd.Parameters.AddWithValue("@licence_ID", licenceID);
                cmd.ExecuteNonQuery();
                dbConnection2.Close();
                MessageBox.Show("Dodano samochód");
            }
        }

        private void AdminAddNewCar_Load(object sender, EventArgs e)
        {

        }

        public static bool checkIfRecordExists(string checkedText, string desiredTable)
        {
            bool result = false;

            SqlConnection dbConnection2 = new SqlConnection("server=Grxai-PC\\witam;" +
                                        "Trusted_Connection=yes;" +
                                        "database=transport.dbo;" +
                                        "connection timeout=30;" +
                                        "Initial Catalog=Transport");

            dbConnection2.Open();

            try
            {
                string sql = "SELECT * FROM " + desiredTable + " WHERE engine_number='" + checkedText + "'";

                SqlCommand cmd = new SqlCommand(sql, dbConnection2);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    result = true;
                    MessageBox.Show("Podany numer silnika juz istnieje w bazie!");
                }
                reader.Close();
            }
            finally
            {
                dbConnection2.Close();
            }

            return result;
        }
    }
}
